/*
 * 项目名称:platform-plus
 * 类名称:StaffServiceImpl.java
 * 包名称:com.platform.modules.staff.service.impl
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2020-10-10 14:34:32        klwu     初版做成
 *
 * Copyright (c) 2019-2019 klwu
 */
package com.platform.modules.custmoer.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.JsonUtils;
import com.platform.common.utils.Query;
import com.platform.modules.custmoer.dao.StaffDao;
import com.platform.modules.custmoer.entity.CustomerEntity;
import com.platform.modules.custmoer.entity.StaffEntity;
import com.platform.modules.custmoer.service.CustomerService;
import com.platform.modules.custmoer.service.StaffService;
import com.platform.modules.httpclient.HttpClient;
import com.platform.modules.httpclient.HttpMethod;
import com.platform.modules.httpclient.HttpParamers;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * Service实现类
 *
 * @author klwu
 * @date 2020-10-10 14:34:32
 */
@Slf4j
@Service("staffService")
public class StaffServiceImpl extends ServiceImpl<StaffDao, StaffEntity> implements StaffService {

    @Autowired
    private CustomerService customerService;

    @Override
    public List<StaffEntity> queryAll(Map<String, Object> params) {
        return baseMapper.queryAll(params);
    }

    @Override
    public Page queryPage(Map<String, Object> params) {
        //排序
        params.put("sidx", "T.CREATE_TIME");
        params.put("asc", false);
        Page<StaffEntity> page = new Query<StaffEntity>(params).getPage();
        List<StaffEntity> staffEntities = baseMapper.selectStaffPage(page, params);
        staffEntities.forEach(new Consumer<StaffEntity>() {
            @SneakyThrows
            @Override
            public void accept(StaffEntity staffEntity) {
                Map<String, Object> custMap = new HashMap<>();
                custMap.put("staffId",staffEntity.getId());
                List<CustomerEntity> customerEntityList = customerService.queryAll(custMap);
                staffEntity.setCustNum(customerEntityList.size());// 客户数
                //staffEntity.setAddress(staffEntity.getProvince()+staffEntity.getCity()+staffEntity.getTown()+staffEntity.getAddress());
                handleOrder(staffEntity);
            }
        });
        return page.setRecords(staffEntities);
    }

    private void handleOrder(StaffEntity staffEntity) throws IOException {
        String phone = staffEntity.getPhone();
        int count = getOrderCountByPhone(phone);
        staffEntity.setOrderNum(count);
    }

    @Override
    public boolean add(StaffEntity staff) {
        return this.save(staff);
    }

    @Override
    public boolean update(StaffEntity staff) {
        return this.updateById(staff);
    }

    @Override
    public boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(String[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

    @Override
    public int getOrderCountByPhone(String phone)  {
        try {
            String url = "http://verify.mengtelaidi.com/api/getOrdersCountByPhone?token=eyJhbGciOiJIUzI1N&phone="+phone;
            HttpParamers httpParamers = new HttpParamers(HttpMethod.GET);
            String result = HttpClient.doGet(url, httpParamers, null, 6000, 6000);
            System.out.println(result);
            JsonObject jsonObj = JsonUtils.getJsonObj(result);
            if(jsonObj.get("code").getAsInt() != 200){
                throw new BusinessException("调用外部接口返回编码="+jsonObj.get("code").getAsInt());
            }
            JsonElement count = jsonObj.get("count");
            return count.getAsInt();
        }catch (Exception e){
            return 0;
        }

    }

    @Override
    public List<StaffEntity> queryByDays(Map<String, Object> params) {
        return queryAll(params);
    }

    public double staffTotal(){
        return baseMapper.staffTotal();
    }
    public double prestaffTotal(){
        return baseMapper.prestaffTotal();
    }
}
